<?php
header("Content-Type: text/html; charset=utf-8",true);
include_once("../../includes/cls.config.php");

class base
{
	public $_sTabela;
		
	public function load( array $aDados )
	{
		$aCampos = get_object_vars( $this );
		foreach( $aCampos as $sFields => $sValue )
		{
			if( $sFields[0] != '_' )
			{
				$this->$sFields = $aDados[ $sFields ];
			}
		}
	}
	public function select( $item = null , $aOrdem = null )
	{
		$sql = "SELECT * FROM " . $this->_sTabela;
		if( ! empty( $item ) ) //FILTRAR O REGISTRO ATRAV�S DO ARGUMENTO
		{
			$sql .= " WHERE id = " . $item;
		}
		if( ! empty( $aOrdem ) ) //ORDERNAR OS REGISTROS
		{
			$sql .= " ORDER BY " . $aOrdem[0] . " " . $aOrdem[1];
		}
		
		//$this->x($sql);
		
		$oMysql = new mysql();
		$pRetorno = $oMysql->qry( $sql ) or die (mysql_error());
		$aSaida = array();
				
		if( ! empty( $item ) )
		{
			$aDados = mysql_fetch_array( $pRetorno , MYSQL_ASSOC );
			$this->load( $aDados );
		}
		else{
			while($aDados = mysql_fetch_array( $pRetorno , MYSQL_ASSOC ) )
			{
				$aSaida[] = $aDados;
			}
			return $aSaida;
		}
	}
	
	public function insert()
	{
		$aCampos = get_object_vars( $this );
		//$this->x($aCampos);
		$sFormFields = '';
		$sFormValues = '';
		foreach( $aCampos as $sFields => $sValues ){
			if( $sFields == 'id' || $sFields == 'imagem' ) continue;
			if( $sFields[0] != '_' ){
				if( $sFields == 'senha' )
				{
					$sFormFields .= $sFields . ',';
					$sFormValues .= md5( $sValues ) . "','";
				}
				elseif( $sFields == 'data' )
				{
					$sFormFields .= $sFields . ',';
					$sFormValues .= $this->formatar_data( $sValues , 'put' ) . "','";
				}
				elseif( $sFields == 'situacao' )
				{
					$sFormFields .= $sFields . ',';
					$sFormValues .= "1" . "','";
				}
				else{
					$sFormFields .= $sFields . ',';
					$sFormValues .= $sValues . "','";
				}
			}
		}
		$sFormFields = substr( $sFormFields, 0, -1 );
		$sFormValues = "'" . substr( $sFormValues, 0, -2 );
		$sql = 'INSERT INTO ' . $this->_sTabela . ' (' . $sFormFields . ') VALUES (' . $sFormValues . ')';
		
		//$this->x($sql);
		
		$oMysql = new mysql();
		return $oMysql->qry( $sql );
	}
	
	public function update( $item )
	{
		$aCampos = get_object_vars( $this );
		//$this->x($aCampos);
		$sCamposSets = '';
		foreach( $aCampos as $sFields => $sValues )
		{
			if( $sFields == 'id' || $sFields == 'situacao' )
			{
				$item = $sValues;
				continue;
			}
			if( $sFields[0] != '_' )
			{
				if( $sFields == 'senha' )
				{
					$sCamposSets .= $sFields . "='" . md5( $sValues ) . "',";
				}
				elseif($sFields == 'data')
				{
					$sCamposSets .= $sFields . "='" . $this->formatar_data( $sValues , 'put' ) . "',";	
				}
				else
				{
					$sCamposSets .= $sFields . "='" . $sValues . "',";
				}
			}
		}
		$sCamposSets = substr( $sCamposSets, 0, -1 ) ;
		$sql = 'UPDATE ' . $this->_sTabela . ' SET ' . $sCamposSets . ' WHERE id=' . $this->id;
		//$this->x($sql);
		$oMysql = new mysql();
		return $oMysql->qry( $sql ) or die (mysql_error());
	}
	
	public function delete( $item, $url, $edicao=null )
	{
		if( empty( $edicao ) )
		{
			$sql = 'DELETE FROM ' . $this->_sTabela . ' WHERE id=' . $item;
			//$this->x($sql);
			$oMysql = new mysql();
			$oMysql->qry( $sql );
			header( 'location:' . $url );
		}
		else
		{
			$sql = "UPDATE revista SET situacao = 0, publicar = 0 WHERE id = '" . $item . "'";
			return $this->_db->qry( $sql );
			header( 'location:' . $url );
		}
	}
	
	public function formatar_data( $data=null , $tipo )
	{
		if( ! empty( $data ) )
		{
			if( $tipo == 'put' )
			{
				$d = explode("/" , $data);
				$d = $d[2] . "-" . $d[1] . "-" . $d[0];
				return $d;
			}
			else
			{
				$d = explode("-" , $data);
				$d = $d[2] . "/" . $d[1] . "/" . $d[0];
				return $d;
			}
		}
	}

	public function x( $obj )
	{
		print '<pre>';
		print_r($obj);
		die;
	}
	
}